package openapi_remote

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/common/http_client"
)

const (
	companyZxListUri         = "/v1/company/zx-list"          //被执行人
	companyCourtRegisterUri  = "/v1/company/court-register"   //立案信息
	companyLawSuitUri        = "/v1/company/law-suit"         //法律诉讼
	companyTaxIllegalListUri = "/v1/company/tax-illegal-list" //税收违法
	companyRickCountUri      = "/v1/company/get-tab-statistics"
)

type openApiRepo struct {
}

func NewCompanyRepo() OpenApiRepoIface {
	return &openApiRepo{}
}

func (c *openApiRepo) GetCompanyDetail(ctx context.Context, keyword string) (*CompanyDetailBo, error) {
	resp := &CompanyDetailBo{}
	if keyword == "" {
		return resp, nil
	}
	path := "/v1/company/detail-info"
	openapiClient := http_client.NewOpenApiHttpClient()
	params := map[string]interface{}{
		"keyword": keyword,
	}
	result := &CompanyDetailResponseBo{}
	_, _, errs := openapiClient.Get(ctx, path, params, nil).EndStruct(result)
	if errs != nil {
		return resp, errs[0]
	}
	if result.Success == false {
		err := errors.New("获取企业信息失败：" + result.Message)
		return resp, err
	}

	return &result.Data, nil
}

func (c *openApiRepo) GetMainDeveloper(ctx context.Context, companyNameList []string) ([]*MainDeveloperItem, error) {
	path := "/v1/big-data-developer/get-main-developer"
	openapiClient := http_client.NewOpenApiHttpClient()
	params := map[string]interface{}{
		"developer": companyNameList,
	}
	result := &MainDataResponseBo{}
	_, _, errs := openapiClient.Post(ctx, path, params, nil).EndStruct(result)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	if result.Success == false {
		err := errors.New("获取企业信息失败：" + result.Message)
		return nil, err
	}

	return result.Data, nil
}

func (c *openApiRepo) AddSyncTask(ctx context.Context, companyNameList []string) ([]string, error) {
	if len(companyNameList) == 0 {
		return nil, nil
	}

	path := "/v1/big-data-developer/add-sync-task"
	openapiClient := http_client.NewOpenApiHttpClient()

	var data []*ApplyDeveloper
	for _, item := range companyNameList {
		data = append(data, &ApplyDeveloper{
			CompanyName: item,
			Source:      "5",
			SourceId:    "",
		})
	}

	params := map[string]interface{}{"type": "kfs", "data": data}

	result := &ApplyDeveloperResponse{}
	_, _, errs := openapiClient.Post(ctx, path, params, nil).EndStruct(result)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	if result.Success == false {
		err := errors.New("添加同步任务失败：" + result.Message)
		return nil, err
	}

	return result.Data.CompanyName, nil
}

// GetCompanyZxList 失信被执行
func (c *openApiRepo) GetCompanyZxList(ctx context.Context, request *GetCompanyRiskCommonRequest) (*GetCompanyZxListData, error) {
	openapiClient := http_client.NewOpenApiHttpClient()
	resp := &GetCompanyZxListResponse{}
	_, _, errs := openapiClient.Post(ctx, companyZxListUri,
		map[string]interface{}{
			"keyword":         request.Keyword,
			"is_return_total": request.IsReturnTotal,
			"page_num":        request.PageNum,
			"page_size":       request.PageSize,
		},
		nil).
		EndStruct(resp)
	if len(errs) > 0 {
		return nil, errs[0]
	}

	return resp.Data, nil
}

// GetCompanyCourtRegister 立案信息
func (c *openApiRepo) GetCompanyCourtRegister(ctx context.Context, request *GetCompanyRiskCommonRequest) (*GetCompanyCourtRegisterData, error) {
	openapiClient := http_client.NewOpenApiHttpClient()
	resp := &GetCompanyCourtRegisterResponse{}
	_, _, errs := openapiClient.Post(ctx, companyCourtRegisterUri,
		map[string]interface{}{
			"keyword":         request.Keyword,
			"is_return_total": request.IsReturnTotal,
			"page_num":        request.PageNum,
			"page_size":       request.PageSize,
		},
		nil).
		EndStruct(resp)
	if len(errs) > 0 {
		return nil, errs[0]
	}

	return resp.Data, nil
}

// GetCompanyLawSuit 法律诉讼
func (c *openApiRepo) GetCompanyLawSuit(ctx context.Context, request *GetCompanyRiskCommonRequest) (*GetCompanyLawSuitData, error) {
	openapiClient := http_client.NewOpenApiHttpClient()
	resp := &GetCompanyLawSuitResponse{}
	_, _, errs := openapiClient.Post(ctx, companyLawSuitUri,
		map[string]interface{}{
			"keyword":         request.Keyword,
			"is_return_total": request.IsReturnTotal,
			"page_num":        request.PageNum,
			"page_size":       request.PageSize,
		},
		nil).
		EndStruct(resp)
	if len(errs) > 0 {
		return nil, errs[0]
	}

	return resp.Data, nil
}

// GetCompanyTaxIllegalList 税收违法
func (c *openApiRepo) GetCompanyTaxIllegalList(ctx context.Context, request *GetCompanyRiskCommonRequest) (*GetCompanyTaxIllegalListData, error) {
	openapiClient := http_client.NewOpenApiHttpClient()
	resp := &GetCompanyTaxIllegalListResponse{}
	_, _, errs := openapiClient.Post(ctx, companyTaxIllegalListUri,
		map[string]interface{}{
			"keyword":         request.Keyword,
			"is_return_total": request.IsReturnTotal,
			"page_num":        request.PageNum,
			"page_size":       request.PageSize,
		},
		nil).
		EndStruct(resp)
	if len(errs) > 0 {
		return nil, errs[0]
	}

	return resp.Data, nil
}

func (c *openApiRepo) GetCompanyRiskCount(ctx context.Context, request *GetCompanyRiskCountRequest) (*GetCompanyRiskCountData, error) {
	openapiClient := http_client.NewOpenApiHttpClient()
	resp := &GetCompanyRiskCountResponse{}
	_, _, errs := openapiClient.Post(ctx, companyRickCountUri,
		map[string]interface{}{
			"keyword":   request.Keyword,
			"tab_codes": request.TabCodes,
		},
		nil).
		EndStruct(resp)
	if len(errs) > 0 {
		return nil, errs[0]
	}

	return resp.Data, nil
}
